<?php

Yii::import('backend.modules.auth.AuthModule');

/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 */
class AuthController extends CController
{
    public $layout='//layouts/column1';
    public $menu = array();
    
    public function beforeAction($action)
    {
        if(parent::beforeAction($action))
        {
            $entryLogined = AuthModule::getEntryLogined();
            $authModule = Yii::app()->getModule('auth');
            $requestUrl = Yii::app()->request->getUrl();
            
            foreach($authModule->alwaysAllows as $allow)
            {
                if($requestUrl == $this->createUrl(array_shift($allow), $allow))
                    return true;
            }

            if($entryLogined)
            {
                if( ! $authModule->getHasInstalled())
                {
                    if($requestUrl != $authModule->getInstallUrl())
                        $this->redirect($authModule->getInstallUrl());
                    else
                        return true;
                }

                foreach($authModule->alwaysAllowsAfterEntryLogined as $allow)
                {
                    $allowUrl = $this->createUrl(array_shift($allow), $allow);
                    if($requestUrl === $allowUrl)
                        return true;
                }

                if(Yii::app()->user->getIsGuest())
                    exit;

                $moduleName = '';
                $module = $this->getModule();
                if($module instanceof CModule)
                    $moduleName = $module->getId();
                
                $access = AuthGenerator::getOperationName($action->getController()->getId(), $action->getId(), $moduleName);
                if(Yii::app()->user->checkAccess($access))
                    return true;
            }
        }   

        AuthModule::setEntryLogined(false);
        $authModule = Yii::app()->getModule('auth');
        $this->redirect($authModule->createUrl('default/index'));
    }
}